package org.xukai.common.util;

import com.google.common.base.Preconditions;

/**
 * @author xukai
 * @desc 字符串模糊处理工具类
 * @date 2017-06-09-14:26
 */
public class StringBlurUtil {

    /**
     * 不模糊开始的nonBlurCharCount个字符
     * @param needBlurStr
     * @param nonBlurCharCount
     * @return
     */
    public static String nonBlurStart(String needBlurStr, int nonBlurCharCount) {
        if (null == needBlurStr || needBlurStr.trim().length() == 0) {
            return needBlurStr;
        }
        if (nonBlurCharCount < 0 ) {
            nonBlurCharCount = 0;
        }
        if (nonBlurCharCount > needBlurStr.length()) {
            nonBlurCharCount = needBlurStr.length();
        }
        Preconditions.checkArgument(nonBlurCharCount < needBlurStr.length(), "模糊字符串出错，字符串");
        char[] chars = needBlurStr.toCharArray();
        for (int i = nonBlurCharCount; i < chars.length; i++) {
            chars[i] = '*';
        }
//        char[] blurChars = new char[5 + 5];
//        System.arraycopy(chars ,0 , blurChars, 0, blurChars.length);
        return String.valueOf(chars);
    }

    /**
     * 不模糊结尾的nonBlurCharCount个字符
     * @param needBlurStr
     * @param nonBlurCharCount
     * @return
     */
    public static String nonBlurEnd(String needBlurStr, int nonBlurCharCount) {
        if (null == needBlurStr || needBlurStr.trim().length() == 0) {
            return needBlurStr;
        }
        if (nonBlurCharCount < 0 ) {
            nonBlurCharCount = 0;
        }
        if (nonBlurCharCount > needBlurStr.length()) {
            nonBlurCharCount = needBlurStr.length();
        }
        char[] chars = needBlurStr.toCharArray();
        for (int i = chars.length - 1 - nonBlurCharCount; i > -1; i--) {
            chars[i] = '*';
        }
//        char[] blurChars = new char[5 + 5];
//        System.arraycopy(chars ,0 , blurChars, 0, blurChars.length);
        return String.valueOf(chars);
    }
}
